import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'

// Custom APIs for renderer
const api = {
  useConfig: (cb) => ipcRenderer.on('useConfig', cb),
  playAudio: (cb) => ipcRenderer.on('playAudio', cb),
  pauseAudio: (cb) => ipcRenderer.on('pauseAudio', cb),
  playSkip: (cb) => ipcRenderer.on('playSkip', cb),
  olderPlay: (cb) => ipcRenderer.on('olderPlay', cb),
  randomPlay: (cb) => ipcRenderer.on('randomPlay', cb),
  loopPlay: (cb) => ipcRenderer.on('loopPlay', cb),
  likePlay: (cb) => ipcRenderer.on('likePlay', cb),
  updateUserLevel: (cb) => ipcRenderer.on('updateUserLevel', cb),
}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
  } catch (error) {
    console.error(error)
  }
} else {
  // @ts-ignore (define in dts)
  window.electron = electronAPI
  // @ts-ignore (define in dts)
  window.api = api
}


export default api
